/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.gzchengguo.cn
* 注意：
* 本软件为www.gzchengguo.cn开发研制，未经购买不得商用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package link.chengguo.orangemall.modules.product.service.impl;

import link.chengguo.orangemall.common.service.impl.BaseServiceImpl;
import link.chengguo.orangemall.modules.product.domain.OrangeStoreProductAttrResult;
import link.chengguo.orangemall.modules.product.service.OrangeStoreProductAttrResultService;
import link.chengguo.orangemall.modules.product.service.mapper.StoreProductAttrResultMapper;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;


/**
* @author chengguo
* @date 2020-05-12
*/
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OrangeStoreProductAttrResultServiceImpl extends BaseServiceImpl<StoreProductAttrResultMapper, OrangeStoreProductAttrResult> implements OrangeStoreProductAttrResultService {

    /**
     * 新增商品属性详情
     * @param map map
     * @param productId 商品id
     */
    @Override
    public void insertOrangeStoreProductAttrResult(Map<String, Object> map, Long productId)
    {
        OrangeStoreProductAttrResult orangeStoreProductAttrResult = new OrangeStoreProductAttrResult();
        orangeStoreProductAttrResult.setProductId(productId);
        orangeStoreProductAttrResult.setResult(JSON.toJSONString(map));
        orangeStoreProductAttrResult.setChangeTime(new Date());

        int count = this.count(Wrappers.<OrangeStoreProductAttrResult>lambdaQuery()
                .eq(OrangeStoreProductAttrResult::getProductId,productId));
        if(count > 0) {
            this.remove(Wrappers.<OrangeStoreProductAttrResult>lambdaQuery()
                    .eq(OrangeStoreProductAttrResult::getProductId,productId));
        }

        this.save(orangeStoreProductAttrResult);
    }


}
